package cn.ecut.config;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 自定义拦截器
 */
public class MyInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
        request.setCharacterEncoding("utf-8");
        //登录页面放行
        if (request.getRequestURI().contains("login")) {
            return true;
        }
        //提交登录放行
        if (request.getRequestURI().contains("toLogin")) {
            return true;
        }
        //提交不能为空
        if (request.getSession().getAttribute("user") != null) {
            return true;
        }
        //验证码放行
        if (request.getRequestURI().contains("checkCode")) {
            return true;
        }
        response.sendRedirect("/views/login.html");
        return false;

    }

    //在目标发放执行之后执行 视图对象返回之前执行
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws IOException {
       /* if (!"胡云江".equals(request.getSession().getAttribute("name"))) {
            response.sendRedirect("/views/login.html");
        }*/
    }

    //在流程都执行完毕后 执行
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {

    }
}
